package com.css.gitstats.rest;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.css.gitstats.domain.GitGroup;
import com.css.gitstats.domain.GitProject;
import com.css.gitstats.domain.GitProjectStatistics;
import com.css.gitstats.domain.vo.GitProjectQueryCriteria;
import com.css.gitstats.service.GitstatsProjectStatistics;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "项目统计")
@RestController
@RequestMapping("${gitstats-context}/projectStatistics")
public class GitstatsProjectStatisticsResource {

	@Resource(name = "${gitstats.service.project-statistics}")
	private GitstatsProjectStatistics gitstatsProjectStatistics;
	
	@ApiOperation("项目提交活动统计")
	@PostMapping("getProjectActivity")
	public ResponseEntity<List<GitProjectStatistics>> getProjectActivity(@RequestBody GitProjectQueryCriteria gitProjectQueryCriteria) {
		List<GitProjectStatistics> gitProjectStatisticses = null;
		try {
			gitProjectStatisticses = gitstatsProjectStatistics.getProjectActivity(gitProjectQueryCriteria);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(gitProjectStatisticses);
		}
		return ResponseEntity.ok(gitProjectStatisticses);
	}
	
	@ApiOperation("查询所有组")
	@GetMapping("findGroups")
	public ResponseEntity<List<GitGroup>> findGroups() {
		List<GitGroup> gitGroups = null;
		try {
			gitGroups = gitstatsProjectStatistics.findGroups();
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(gitGroups);
		}
		return ResponseEntity.ok(gitGroups);
	}
	
	@ApiOperation("根据组id或路径查询指定的组")
	@GetMapping("getGroupByIdOrPath/{getGroupByIdOrPath}")
	public ResponseEntity<GitGroup> getGroupByIdOrPath(@PathVariable Object groupIdOrPath) {
		GitGroup gitGroup = null;
		try {
			gitGroup = gitstatsProjectStatistics.getGroupByIdOrPath(groupIdOrPath);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(gitGroup);
		}
		return ResponseEntity.ok(gitGroup);
	}
	
	@ApiOperation("查询所有项目")
	@GetMapping("findProject")
	public ResponseEntity<List<GitProject>> findProject() {
		List<GitProject> gitProjects = null;
		try {
			gitProjects = gitstatsProjectStatistics.findProject();
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(gitProjects);
		}
		return ResponseEntity.ok(gitProjects);
	}
	
	@ApiOperation("根据项目id或路径查询指定的项目")
	@GetMapping("getProjectsByIdOrPath/{projectIdOrPath}")
	public ResponseEntity<GitProject> getProjectsByIdOrPath(@PathVariable Object projectIdOrPath) {
		GitProject gitProject = null;
		try {
			gitProject = gitstatsProjectStatistics.getProjectsByIdOrPath(projectIdOrPath);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(gitProject);
		}
		return ResponseEntity.ok(gitProject);
	}
	
	@ApiOperation("根据组ID或路径查询下面所有项目")
	@GetMapping("getProjectsByGroupIdOrPath/{groupIdOrPath}")
	public ResponseEntity<List<GitProject>> getProjectsByGroupIdOrPath(@PathVariable Object groupIdOrPath) {
		List<GitProject> gitProjects = null;
		try {
			gitProjects = gitstatsProjectStatistics.getProjectsByGroupIdOrPath(groupIdOrPath);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(gitProjects);
		}
		return ResponseEntity.ok(gitProjects);
	}
}
